package kyssion.leetcode.num101_150;

import kyssion.leetcode.util.TreeNode;

public class code108_将有序数组转换为二叉搜索树 {
    public static void main(String[] args) {
        TreeNode treeNode = new code108_将有序数组转换为二叉搜索树()
                .sortedArrayToBST(new int[]{
                        -10,-3,0,5,9
                });
        System.out.println();
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        return ans(nums,0,nums.length-1);
    }

    private TreeNode ans(int[] nums, int start, int end) {
        if(start>end){
            return null;
        }
        // 0 1 0
        int midle = (start+end)>>1;
        TreeNode treeNode = new TreeNode(nums[midle]);
        treeNode.left = ans(nums,start,midle-1);
        treeNode.right = ans(nums,midle+1,end);
        return treeNode;
    }
}
